package com.mohe.shanpao.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class EchoServerHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//		ByteBuf in = (ByteBuf)msg;
//		
//		ByteBuf out = ctx.alloc().buffer();
//		out.discardReadBytes();
//		out.writeBytes(in);
//		ctx.write(out);
//		ctx.flush();
		
//		ChannelFuture future = ctx.writeAndFlush(msg+ "\n");
		
		ctx.writeAndFlush(msg);
		
		Message m = (Message)msg;
		System.out.println("server: "+m.getTimestamp() + " ===== "+m.getValue());
		
	}


}
